const app = getApp()
import {
  theOrder,
  printingSubmit
} from '../../config/https'; 
//订单列表、确认打印提交
let Tool = require('../../config/tool.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTab: 1,
    orderList: [],
    page: 0,
    AllSelect: '',      //全选
    selectArr: []       //选中的订单id
  },
  tabClick: Tool.debounce(function(e){
    let type = e.currentTarget.dataset.type
    if(type == this.data.showTab) return
    this.setData({
      showTab: type,
      page: 0,
      orderList: [],
      selectArr: []
    })
    this.getData(type)
  }, 200),
  getData(status){
    let this_ = this
    wx.showLoading({
      title: '加载中'
    })
    theOrder({
      storeid: wx.getStorageSync('storeid'),
      page: ++this_.data.page, //页数
      status,                  //待自提：1 || 已完成: 3
      dispatchtype: 1
    }).then(res => {
      wx.hideLoading()
      res.list.forEach(item => {
        item.checkbox = true
      })
      this.setData({
        orderList: [...this.data.orderList,...res.list]
      })
      this.chkAll()
    })
  },
  //单选选中 || 单选取消
  selectChange(e){
    let index = e.currentTarget.dataset.index
    let Arr = []         //选中的订单id
    let orderList = this.data.orderList
    let radioChk = `orderList[${index}].checkbox`
    orderList[index].checkbox = e.detail
    let chkAll = orderList.every(item => {
      return item.checkbox
    })
    orderList.map(item => {
      if(item.checkbox){
        Arr.push(item.id)
      }
    })
    this.setData({
      AllSelect: chkAll
    })
    this.setData({
      [radioChk]: e.detail,
      selectArr: Arr
    })
  },
  //全部选中 || 全部取消
  onAllChange(e){
    let orderList = this.data.orderList
    let Arr = []     //选中的订单id
    orderList.map(item => {
      item.checkbox = e.detail
      if(e.detail){
        Arr.push(item.id)
      }
    })
    this.setData({
      orderList,
      AllSelect: e.detail,
      selectArr: Arr
    })
  },
  chkAll(){
    let Arr = []     //选中的订单id
    let orderList = this.data.orderList
    let chkAll = orderList.every(item => {
      return item.checkbox
    })
    orderList.map(item => {
      if(item.checkbox){
        Arr.push(item.id)
      }
    })
    this.setData({
      AllSelect: chkAll,
      selectArr: Arr
    })
  },
  goDetails(e){
    let {id,type}= e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/selfRaisingDetails/selfRaisingDetails?orderid=' + id + "&type=" + type
    })
  },
  //确认提货打印
  goSubmit(){
    let Arr = this.data.selectArr
    if(Arr.length == 0){
      app.$tip('请选择的打印订单')
      return
    }
    wx.showLoading({
      title: '加载中'
    })
    printingSubmit({
      storeid: wx.getStorageSync('storeid'),
      orderId: Arr.join(',')
    }).then(res => {
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/selfRaisingResult/selfRaisingResult?resultType=1' + '&orderId=' + Arr.join(','),
      })
    }).catch(err => {
      wx.hideLoading()
      app.$tip(err.message)
      if(err.code == 401){
       setTimeout(() => {
         wx.navigateTo({
           url: '/pages/selfRaisingResult/selfRaisingResult?resultType=2' + '&orderId=' + Arr.join(','),
         })
       })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!wx.getStorageSync('authKey')) {
      app.$tip('请先登入~~')
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/logs/logs',
        })
      }, 1500)
      return
    }
    this.setData({
      page: 0,
      orderList: [],
      selectArr: []
    })
    this.getData(this.data.showTab)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})